﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//就不搞这么复杂了，所有方法我都放在这个类里面

namespace MyConsoleApp1
{
    class Print
    {
        


        //只能读取的查询，这个方法只能读取不能修改
        public static void PrintSQL(SqlConnection sqlConnection)
        {
            SqlCommand sqlCommand = new SqlCommand("select * from Student", sqlConnection);
            var dr = sqlCommand.ExecuteReader();
            while (dr.Read())
            {
                Console.WriteLine($"{dr[0]}\t{dr[1]}\t{dr[2]}");
            }
        }

        //添加数据库数据
        public static void AddSQL(SqlConnection sqlConnection)
        {
            Console.WriteLine("请输入添加的姓名");
            String  addName = Console.ReadLine();  //用户输入添加的姓名
            Console.WriteLine("请添加年龄");
            var  AddAge = int.Parse(Console.ReadLine()); //用户输入班级
            var sql = string.Format("insert into Student(Name,Age) values ('{0}',{1})", addName, AddAge);

            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            Console.WriteLine(sqlCommand.ExecuteNonQuery());
            sqlCommand.ExecuteNonQuery();

        }
        //删除数据库数据
        public static void DeleteSQL(SqlConnection sqlConnection)
        {
            Console.WriteLine("请输入要删除的ID号");
            var DeleteId =int.Parse(Console.ReadLine());
            var sql = $"DELETE FROM student where Id = {DeleteId}";
            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            Console.WriteLine(sqlCommand.ExecuteNonQuery());
            sqlCommand.ExecuteNonQuery();
        }
        
    }
}
